
<script setup lang="ts">
import { reactive, ref,onMounted } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { Search,Refresh,CloseBold,Plus,UploadFilled,Edit} from '@element-plus/icons-vue'
import uploadDiglog from '../components/uploadDiglog.vue'
import listUploadDiglog from '../components/listUploadDiglog.vue'
import {courseList,deleteCourse, deleteCourseList } from '../api/uploadVideoList'
import { searchTeacher} from '../api/uploadVideo'
import { getHour } from '../utils/date'
import locale from 'element-plus/lib/locale/lang/zh-cn'
interface RuleForm {
  name: string
  lecturerName: string
  phone: string
  pageNum:number,
  pageSize:number,
  total:number,
  data_id:string
}
interface User {
  class_name: string |null
  class_time: string |null
  class_teacher: string |null,
  teacher_phone:string | null,
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
let options:any = ref([])
const ruleForm = reactive<RuleForm>({
  name: '',
  lecturerName: '',
  phone: '',
  pageNum:1,
  pageSize:10,
  total:0,
  data_id :""
})
let tableData:any= ref([])
const rules = reactive<FormRules<RuleForm>>({
  
})
//初始化
const getCourse = () =>{
  let params = {
    class_name:ruleForm.name,
    class_teacher:ruleForm.lecturerName,
    teacher_phone:ruleForm.phone,
    pageNum:ruleForm.pageNum,
    pageSize:ruleForm.pageSize,
    data_id :ruleForm.data_id 
  }
  courseList(params).then((res:any)=>{
      if(res.code === '200'){
        tableData.value = res.data.list || []
        ruleForm.total = res.data.total
      }else{
        tableData.value = []
      }
  })
}
const getTeacherList = () => {
    searchTeacher({ name: '', phone: '' }).then(res => {
        if (res) {
            console.log(res)
            options.value = res.list || []
        }
    })
}
onMounted(()=>{
  getTeacherList()
  getCourse()
})
//编辑
const showDiglog = ref(false)
const statusTitle = ref('单条上传')
const courseInfo = ref({})
const openDiglogOne = (status:string,item:any) => {
  courseInfo.value = JSON.parse(JSON.stringify(item))
  console.log(courseInfo.value)
  statusTitle.value = status
  showDiglog.value = true
}
const closeDiglogOne = () =>{
   showDiglog.value = false
   getCourse()
}

//多条上传
const listShowDislog = ref(false)
const openDiglogList = () => {
   listShowDislog.value = true
}
const closeDiglogList = () => {
   listShowDislog.value = false
   getCourse()
}


//删除课时(单个)
const deleteOne = (id: string) => {
    let data: any = localStorage.getItem('token')
    let { teacher_name, phone } = JSON.parse(data)
    let params = {
        class_number:id,
        updateUserName: teacher_name,
        updateUsserPhone: phone
    }

    deleteCourse(params).then(res => {
        if (res.code === '200') {
          ElMessage.success('删除成功')
          getCourse()
        }else{
          ElMessage.error(res.msg)
        }
    })
}
//多选
const idList = ref([])
const handleSelectionChange = (val: any) => {
    idList.value = val
}
//批量删除
const deleteAll = () => {
    if (idList.value.length === 0) {
        ElMessage.error('请选择课时')
        return
    }
    let data: any = localStorage.getItem('token')
    let { teacher_name, phone } = JSON.parse(data)
    let params = {
        ids: idList.value.map(item => item.class_number).join(','),
        updateUserName: teacher_name,
        updateUsserPhone: phone
    }
    console.log(params)
    deleteCourseList(params).then(res => {
        if (res.code === '200') {
          ElMessage.success('删除成功')
          getCourse()
        }else{
          ElMessage.error(res.msg)
        }
    })
}

//改变每页多少条
const handleSizeChange = (val: number) => {
    ruleForm.pageSize = val
    // ruleForm.data_id = tableData[length-1]._id
    getCourse()
}
//点击第几页
const handleCurrentChange = (val: number) => {
   ruleForm.pageNum = val
   getCourse()
}
//搜索
const searchList = () => {
  getCourse()
}
//重置 
const reset = () => {
    ruleForm.name = ''
    ruleForm.lecturerName =''
    ruleForm.phone = ''
    ruleForm.pageNum = 1
    ruleForm.pageSize = 10
    ruleForm.data_id = ''
    getCourse()
}
</script>

<template>
  <div class="container" style="padding: .5rem;">
    <div class="top-search">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="atuo" class="demo-ruleForm"
        :size="formSize" status-icon>
        <el-row :gutter="6">
          <el-col :span="4">
            <el-form-item label="课时名称" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="讲师名称" prop="lecturerName">
              <el-select v-model="ruleForm.lecturerName" placeholder="请选择讲师" style="width: 100%;">
              <el-option
                v-for="item in options"
                :key="item.phone"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="讲师电话" prop="phone">
              <el-input v-model="ruleForm.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-form-item >
              <el-button type="primary" :icon="Search" style="width: 100%;" @click="searchList">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-form-item >
              <el-button type="warning" :icon="Refresh" style="width: 100%;" @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="uploadButton">
      <!-- <el-button type="primary" :icon="Plus" @click="openDiglogOne('单条上传')">单条上传</el-button> -->
      <el-button type="success" :icon="UploadFilled" @click="openDiglogList">批量上传</el-button>
      <el-button type="danger" :icon="CloseBold" @click="deleteAll">批量删除</el-button>
    </div>
    <div class="table-data" style="padding-top: 0.1rem;">
      <el-table :data="tableData" style="width: 100%"  @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" />
    <el-table-column prop="class_name" label="课时名称" />
    <el-table-column  label="课时时长" >
        <template #default="scope">
            {{ getHour(scope.row.class_time) }}
        </template>
    </el-table-column>
    <el-table-column prop="class_teacher" label="讲师姓名" />
    <el-table-column prop="teacher_phone" label="讲师手机号" />
    <el-table-column prop="upload_name" label="上传人" />
    <el-table-column prop="upload_time" label="上传时间" />
    <el-table-column fixed="right" label="操作" width="400">
      <template #default="scope">
        <el-button type="primary" :icon="Edit" @click="openDiglogOne('编辑',scope.row)">编辑</el-button>
        <el-button type="danger" :icon="CloseBold" @click="deleteOne(scope.row.class_number)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    </div>
    <div class="button" style="padding-top: 1rem;">
      <el-config-provider :locale="locale">
        <el-pagination
      v-model:current-page="ruleForm.pageNum"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 30, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="ruleForm.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
      </el-config-provider>
    </div>
     <!-- 单条上传 -->
      <uploadDiglog :dialogVisible="showDiglog" @close-diglog="closeDiglogOne" :status="statusTitle" :courseInfo="courseInfo"></uploadDiglog>
      <!-- 多条上传 -->
      <listUploadDiglog :listDialogVisible="listShowDislog" @close-diglog="closeDiglogList"></listUploadDiglog>
  </div>
</template>

<style scoped></style>
